package com.fr.web;

import com.fr.base.core.html.Tag;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

public class Browser
{
  private String userAgent;
  private String browser;
  private String version;
  private static Pattern[] uaPatterns = { Pattern.compile("(webkit)[ \\/]([\\w.]+)"), Pattern.compile("(opera)(?:.*version)?[ \\/]([\\w.]+)"), Pattern.compile("(msie) ([\\w.]+)"), Pattern.compile("(mozilla)(?:.*? rv:([\\w.]+))?") };

  public static Browser resolve(HttpServletRequest paramHttpServletRequest)
  {
    return resolve(paramHttpServletRequest.getHeader("user-agent"));
  }

  public static Browser resolve(String paramString)
  {
    paramString = paramString.toLowerCase();
    Browser localBrowser = new Browser();
    localBrowser.userAgent = paramString;
    for (int i = 0; i < uaPatterns.length; ++i)
    {
      Matcher localMatcher = uaPatterns[i].matcher(paramString);
      if (localMatcher.find())
      {
        localBrowser.browser = localMatcher.group(1);
        localBrowser.version = localMatcher.group(2);
        break;
      }
    }
    return localBrowser;
  }

  public String getUserAgent()
  {
    return this.userAgent;
  }

  public String getBrowser()
  {
    return this.browser;
  }

  public String getVersion()
  {
    return this.version;
  }

  private boolean isIE()
  {
    return "msie".equals(this.browser);
  }

  private boolean isOpera()
  {
    return "opera".equals(this.browser);
  }

  private boolean isWebkit()
  {
    return "webkit".equals(this.browser);
  }

  private boolean isFireFox()
  {
    return "mozilla".equals(this.browser);
  }

  private boolean versionLessThan(String paramString)
  {
    String[] arrayOfString1 = this.version.split("\\.");
    String[] arrayOfString2 = paramString.split("\\.");
    int i = Math.max(arrayOfString2.length, arrayOfString1.length);
    for (int j = 0; j < i; ++j)
    {
      int k = (arrayOfString1.length > j) ? Integer.parseInt(arrayOfString1[j]) : 0;
      int l = (arrayOfString2.length > j) ? Integer.parseInt(arrayOfString2[j]) : 0;
      if (k < l)
        return true;
    }
    return false;
  }

  private boolean versionLargerThan(String paramString)
  {
    String[] arrayOfString1 = this.version.split("\\.");
    String[] arrayOfString2 = paramString.split("\\.");
    int i = Math.max(arrayOfString2.length, arrayOfString1.length);
    for (int j = 0; j < i; ++j)
    {
      int k = (arrayOfString1.length > j) ? Integer.parseInt(arrayOfString1[j]) : 0;
      int l = (arrayOfString2.length > j) ? Integer.parseInt(arrayOfString2[j]) : 0;
      if (k > l)
        return true;
    }
    return false;
  }

  public String getEncodedFileName4Download(String paramString)
    throws UnsupportedEncodingException
  {
    if (isIE())
      return URLEncoder.encode(paramString, "UTF-8");
    return new String(paramString.getBytes("UTF-8"), "ISO8859-1");
  }

  public boolean supportTransparentImage()
  {
    return ((!(isIE())) || (!(versionLessThan("7.0"))));
  }

  public boolean shouldSetContentTypeOnZipDownload()
  {
    return (!(isIE()));
  }

  public boolean supportGzip()
  {
    return (!(isIE()));
  }

  public void setHrefOnArea(Tag paramTag)
  {
    if (!(isFireFox()))
      if ((isIE()) && (!(versionLessThan("6.0"))))
        paramTag.attr("href", "###");
      else
        paramTag.attr("href", "javascript:void(0)");
  }

  public boolean shouldSetOverflowAsHiddenOnSheetContainer()
  {
    return (!(isFireFox()));
  }

  public boolean shouldWrapCenter()
  {
    return isIE();
  }

  public boolean shouldModifyWidth4PainterAccordingToBorderWidth()
  {
    return isIE();
  }

  public boolean shouldDoSomeStuff4MissingRightBorderOfTable()
  {
    return isWebkit();
  }

  public boolean justDealWithTheHiddenRowsOfTail()
  {
    return isIE();
  }

  public boolean shouldConsiderHeavyTD()
  {
    return ((!(isIE())) || (versionLessThan("6.0")));
  }

  public boolean supportIMAGEBase64Decode()
  {
    return (!(isIE()));
  }
}